import { plugin, getPathBuffer, AMessage } from '../../api/api'
import {
  _path,
  existplayer,
  bound,
  _dirname,
  _item,
  path,
  Write_iteam,
  Write_player,
  _yaml
} from '../../mode/allapi'
import {setenmy, zhencha} from '../resource/challengeapi'
const useraction = []

export class map extends plugin {
  constructor() {
    super({
      dsc: '资源',
      rule: [
        {
          reg: /^(#|\/)?移兵.*$/,
          fnc: 'going'
        },
        {
          reg: /^(#|\/)?侦察.*$/,
          fnc: 'scouting'
        },
        {
          reg: /^(#|\/)?地图*$/,
          fnc: 'map'
        },
        {
          reg: /^(#|\/)?我的城池*$/,
          fnc: 'mycity'
        },
        {
          reg: /^(#|\/)?返回*$/,
          fnc: 'backing'
        }
      ]
    })
  }
  async going(e:AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const time = 120000
    const didian = e.msg.replace(/(#|\/)?移兵/, '')
    const place = await _item(id, 'place')
    const player = await (await _path(id)).player
    const action = await bound(didian, place, 'name')
    if (player.garrisons == 1) return e.reply(`军队驻守中`)
    if (!action) return e.reply(`未发现名为${didian}的地点`)
    const link = action.link.split(',')
    if (useraction[id]) {
      return e.reply('您的军队正在行军中，请稍等片刻。')
    }
    const linkname = link.includes(player.position)
    if (player.position == 0) {
      return e.reply('您还未攻打过城池，可任选城池攻占，无需行军。')
    } else if (player.position == didian) {
      return e.reply('您的军队已在此地')
     }  else if (!linkname && action.belong != id&&action.garrisons!='0') {
      return e.reply('您的军队距离此地较远')
    }
    useraction[id] = setTimeout(async () => {
      player.position = didian
      player.state = 0
      Write_player(id, player)
      e.reply(`${e.segment.at(id)}成功到达${didian}`)
      clearTimeout(useraction[id])
      useraction[id] = null
    }, time)
    player.state = 1
    Write_player(id, player)
    e.reply(`正在前往预计${time / 1000}秒后到达`)
  }
  async map(e: AMessage) {
    if (e.isGroup != true) return false
    const pathfile = path.join(
      'plugins',
      'Tkdream-1.2',
      'resources',
      'assets',
      'img',
      'map',
      'map.jpg'
    )
    const img = getPathBuffer(pathfile)
    e.reply(img)
  }

  async scouting(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const player = await (await _path(id)).player
    const didian = e.msg.replace(/(#|\/)?侦察/, '')
    const place = await _item(id, 'place')
    const action = await bound(didian, place, 'name')
    if (!action) return e.reply(`无法查询${didian}的信息`)
    if (player.user_id == action.belong) {
      return e.reply(`无法侦察己方城池`)
    }
    await setenmy (action,id)
    e.reply(await zhencha (action))
    Write_iteam(id, 'place', place)
  }

  async mycity(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const place = await _item(id, 'place')
    const city = place.filter(city => city.belong == id).map(city => city)
    const cityNames = []
    for (let i = 0; i < city.length; i++) {
      cityNames.push(city[i].name)
    }
    if (cityNames.length == 0) {
      e.reply('主公暂未占领城池')
    } else {
      e.reply(`主公势力范围：${cityNames.join(', ')}`)
    }
  }
  async backing(e: AMessage) {
    const id = e.user_id
    if (!e.isGroup || !(await existplayer(id))) return false
    if (!useraction[id]) return e.reply('军队空闲中')
    clearTimeout(useraction[id])
    useraction[id] = null
    const player = await (await _path(id)).player
    player.state = 0
    Write_player(id, player)
    e.reply('返回成功')
  }
}
